package com.pbc.gmp.commons.utils;

import com.pbc.gmp.model.MenuTreeResourseDto;
import com.pbc.gmp.model.OrgTreeDto;
import com.pbc.gmp.model.basemodel.Org;
import com.xiaoleilu.hutool.convert.Convert;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class TreeUtils {

    public static OrgTreeDto org2tree( List<OrgTreeDto> orgList){

        OrgTreeDto orgTreeDto = null;
        for (OrgTreeDto treeNode : orgList) {
            if (treeNode.getPid()== Convert.toLong(0)) {
                orgTreeDto=   findChildren(treeNode,orgList);
            }
        }
       return orgTreeDto;

    }

    public static OrgTreeDto org2tree( List<OrgTreeDto> orgList,Long rootid){

        OrgTreeDto orgTreeDto = null;
        for (OrgTreeDto treeNode : orgList) {
            if (treeNode.getId()== Convert.toLong(rootid)) {
                orgTreeDto=   findChildren(treeNode,orgList);
            }
        }
        return orgTreeDto;

    }
    public static List<OrgTreeDto> orgtree2list( List<OrgTreeDto> orgList,Long rootid){

        List<OrgTreeDto>orgTreeDtoList = new ArrayList<OrgTreeDto>();

        for (OrgTreeDto treeNode : orgList) {
            if (treeNode.getId()== Convert.toLong(rootid)) {
                orgTreeDtoList.addAll( findChildren2List(orgList,treeNode.getId()))  ;
            }

        }
        return orgTreeDtoList;

    }
    public static OrgTreeDto org2menutree( List<OrgTreeDto> orgList,Long rootid){

        OrgTreeDto orgTreeDto = null;
        for (OrgTreeDto treeNode : orgList) {
            if (treeNode.getId()== Convert.toLong(rootid)) {
                orgTreeDto=   findChildren(treeNode,orgList);
            }
        }
        return orgTreeDto;

    }
    public static OrgTreeDto findChildren(OrgTreeDto treeNode,List<OrgTreeDto> treeNodes) {
        for (OrgTreeDto it : treeNodes) {
            if(treeNode.getId().equals(it.getPid())) {
                if (treeNode.getChildren() == null) {
                    treeNode.setChildren(new ArrayList<OrgTreeDto>());

                }else {
//                   treeNode.setState("open");
                }

                treeNode.getChildren().add(findChildren(it,treeNodes));
            }
        }
        return treeNode;
    }
    public static List<OrgTreeDto> findChildren2List( List<OrgTreeDto> orgList,Long rootId){
        List<OrgTreeDto> otdlist=new ArrayList<OrgTreeDto>();
        for (OrgTreeDto it : orgList) {
          if(it.getPid()==rootId){
              otdlist.add(it);
              otdlist.addAll(findChildren2List(orgList,it.getId()));
          }
        }
        return otdlist;
    }
    ///////////////////////////////////////////////////////
    public static Set<MenuTreeResourseDto> menu2tree(Set<MenuTreeResourseDto> menulist){
        Set<MenuTreeResourseDto> menuTreeResourseDtoList=new HashSet<MenuTreeResourseDto>();


        for (MenuTreeResourseDto treeNode : menulist) {
            if (treeNode.getPid()== Convert.toLong(0)) {
                MenuTreeResourseDto ds=findMenuChildren(treeNode,menulist);
                if (ds == null) {

                }else {
                    menuTreeResourseDtoList.add(ds);
                }

            }
        }
        return menuTreeResourseDtoList;

    }
    public static MenuTreeResourseDto findMenuChildren(MenuTreeResourseDto treeNode,Set<MenuTreeResourseDto> treeNodes) {
        for (MenuTreeResourseDto it : treeNodes) {
            if(treeNode.getId()==it.getPid()) {
                if (treeNode.getChildren() == null) {
                    treeNode.setChildren(new ArrayList<MenuTreeResourseDto>());
                }
                treeNode.getChildren().add(findMenuChildren(it,treeNodes));
            }
        }
        return treeNode;
    }
}